﻿@{
    ViewData["Title"] = "文件管理";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-fluid">
                <button type="button" class="layui-hide" id="upload-file"></button>
                <div id="fileManager" lay-filter="test"></div>
            </div>

        </div>
    </div>
</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['tools', 'toast', 'http', 'fileManager'], function () {
            var $ = layui.$, form = layui.form, upload = layui.upload,
                http = layui.http, table = layui.table,
                toast = layui.toast,
                tools = layui.tools, fileManager = layui.fileManager;

            app.init = function () {
                //
                app.uploadIns = upload.render({
                    elem: '#upload-file',
                    url: '/api/sysFile/uploadFile',
                    accept: 'file',
                    done: function (res) {
                        toast.success({ message: '上传成功！' });
                        table.reload('lsttable');
                    }
                });

                fileManager.render({
                    elem: '#fileManager'
                    , method: 'post'
                    , contentType: 'application/json'
                    , id: 'fmTest'
                    , btn_upload: true
                    , btn_create: true
                    , url: '/api/sysFileEx/page'
                    , parseData: function (res) {
                        let _res = [];
                        _res.code = 0;
                        _res.data = res.result.items;
                        _res.count = res.result.total
                        return _res;
                    }
                    , done: function (res, curr, count) {
                        // console.log(res,curr,count)
                    }
                    , page: { limit: 12 }
                });
                //监听图片选择事件
                fileManager.on('pic(test)', function (obj) {
                    //obj.obj 当前对象
                    //obj.data 当前图片数据
                    var data = obj.data;
                    layer.alert(JSON.stringify(data), {
                        title: '当前数据：'
                    });
                });
                //监听图片上传事件
                fileManager.on('uploadfile(test)', function (obj) {
                    console.log(obj)
                    $('#upload-file').trigger('click');
                });
                //监听新建文件夹事件
                fileManager.on('new_dir(test)', function (obj) {
                    $.post('/api/sysFileEx/folder', { 'name': obj.folder, 'pid': obj.pid }, function (res) {
                        if (res.code == 200) {
                            fileManager.reload('fmTest');
                        }
                        else {
                            layer.msg(res.message);
                        }
                    })
                });
            }();
            //
            app.del = function (id) {
                var index = layer.confirm('您确定要删除当前项吗？', function () {
                    http.post('/api/sysFile/delete', { id }).then((res) => {
                        if (res.code == 200) {
                            layer.close(index);
                            table.reload('lsttable');
                            toast.success({ message: '删除成功！' });
                        }
                        else {
                            layer.alert('删除失败，错误信息' + res.message, { icon: 2 });
                        }
                    })
                });
            }
        })
    </script>
}
